package cn.edu.tsinghua.ie.ieFunds.controller;

import cn.edu.tsinghua.ie.ieFunds.bean.base.Stock;
import cn.edu.tsinghua.ie.ieFunds.service.StockService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    public Page<Stock> getPage(@RequestParam(value = "stockName") String stockName,
                               @RequestParam(value = "page", defaultValue = "0") Integer page,
                               @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                               @RequestParam(value = "pageSortField", required = false) String pageSortField,
                               @RequestParam(value = "pageSortDirection", defaultValue = "DESC") String pageSortDirection) {
        Pageable pageable;

        // Sort
        Sort sort = null;
        if (pageSortField != null) {
            if (pageSortDirection.compareToIgnoreCase("asc") == 0) {
                sort = new Sort(Sort.Direction.ASC, pageSortField);
            } else if (pageSortDirection.compareToIgnoreCase("desc") == 0) {
                sort = new Sort(Sort.Direction.DESC, pageSortField);
            }
        } else {
            sort = new Sort(Sort.Direction.ASC, "name");
        }

        pageable = new PageRequest(page, pageSize, sort);
        return stockService.getPage(stockName, pageable);
    }

    @RequestMapping(value = "/getDistinctPage", method = RequestMethod.GET)
    public Page<Stock> getDistinctPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                       @RequestParam(value = "pageSortField", required = false) String pageSortField,
                                       @RequestParam(value = "pageSortDirection", defaultValue = "DESC") String pageSortDirection) {
        Pageable pageable;

        // Sort
        Sort sort = null;
        if (pageSortField != null) {
            if (pageSortDirection.compareToIgnoreCase("asc") == 0) {
                sort = new Sort(Sort.Direction.ASC, pageSortField);
            } else if (pageSortDirection.compareToIgnoreCase("desc") == 0) {
                sort = new Sort(Sort.Direction.DESC, pageSortField);
            }
        } else {
            sort = new Sort(Sort.Direction.ASC, "name");
        }

        pageable = new PageRequest(page, pageSize, sort);
        return stockService.getDistinctPage(pageable);
    }

    @RequestMapping(value = "/getDistinctList", method = RequestMethod.GET)
    public List<Stock> getDistinctList() {
        return stockService.getDistinctList();
    }
}
